<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <title>动态改变文档内容</title>
  <script>
      let now = new Date();
      let year = now.getFullYear();
      let month = String(now.getMonth() + 1).padStart(2, '0'); // 月份是从0开始的  
      let day = String(now.getDate()).padStart(2, '0');
      let hours = String(now.getHours()).padStart(2, '0');
      let minutes = String(now.getMinutes()).padStart(2, '0');
      let seconds = String(now.getSeconds()).padStart(2, '0');
      let nowStr = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
      console.log(nowStr); // 输出类似：2023-06-15 10:47:30
      function displayDate () {
      document.getElementById("currentTime").innerHTML= nowStr;
      var x = document.getElementsByTagName("h1");
      x[0].innerHTML = "欢迎来到我的网站";
    }
  </script>
</head>

<body>
  <h1>初始标题</h1>
  <button id="changeTitle" onclick="displayDate()">改变标题</button>
  <div id="currentTime"></div>
</body>

</html>
